
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: Sidekar.java 317 2010-02-11 22:06:51Z archie.cobbs $
 */

package org.dellroad.sidekar;

import org.aspectj.lang.JoinPoint;

/**
 * Sidekar static fields and utility methods.
 */
public final class Sidekar {

    /**
     * Name to use for the implicit field that contains an item's {@link Entity} (<code>{@value}</code>).
     * Values given to {@link Index} access methods should be {@link Entity} objects.
     *
     * @see Item#getSidekarEntity
     */
    public static final String ENTITY_FIELD_NAME = "sidekarEntity";

    /**
     * Name to use for the implicit field that contains an item's unique ID (<code>{@value}</code>).
     * Values given to {@link Index} access methods should be {@link Long} objects.
     *
     * @see Item#getSidekarId
     */
    public static final String ID_FIELD_NAME = "sidekarId";

    /**
     * Name to use for the implicit field that contains an item's attached status (<code>{@value}</code>).
     * This implicit field may not be indexed.
     *
     * @see Item#isSidekarAttached
     */
    public static final String ATTACHED_FIELD_NAME = "sidekarAttached";

    private Sidekar() {
    }

    /**
     * Verify the name is a valid Sidekar entity name.
     * A name is valid if it is a valid Java identifier.
     *
     * @throws IllegalArgumentException if name is invalid
     */
    public static void checkValidEntityName(String name) {
        Sidekar.checkName(name, "entity");
    }

    /**
     * Verify the name is a valid Sidekar field name.
     * A name is valid if it is a valid Java identifier.
     * Valid names may not conflict with the implicit field names defined by the {@link Item} interface Java bean properties.
     *
     * @throws IllegalArgumentException if name is invalid
     */
    public static void checkValidFieldName(String name) {
        Sidekar.checkName(name, "field");
        if (name.equals(ENTITY_FIELD_NAME)
          || name.equals(ID_FIELD_NAME)
          || name.equals(ATTACHED_FIELD_NAME))
            Sidekar.invalid(name, "field");
    }

    /**
     * Verify the name is a valid Sidekar index name.
     * A name is valid if it is a valid Java identifier.
     *
     * @throws IllegalArgumentException if name is invalid
     */
    public static void checkValidIndexName(String name) {
        Sidekar.checkName(name, "index");
    }

    private static void checkName(String name, String type) {
        if (name == null)
            throw new IllegalArgumentException("illegal null " + type + " name");
        if (!name.matches("\\p{javaJavaIdentifierStart}[\\p{javaJavaIdentifierPart}]*"))
            Sidekar.invalid(name, type);
    }

    private static void invalid(String name, String type) {
        throw new IllegalArgumentException("illegal " + type + " name `" + name + "'");
    }

// Aspect hooks

    static boolean getBooleanFieldValue(JoinPoint.StaticPart joinPoint, long id) {
        System.out.println("**** getBooleanFieldValue(): id="+id+" joinPoint="+joinPoint);
        return true;
    }

    static void setBooleanFieldValue(JoinPoint.StaticPart joinPoint, long id, boolean value) {
        System.out.println("**** setBooleanFieldValue(): id="+id+" value="+value+" joinPoint="+joinPoint);
    }
}

